﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace loginapp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void init()
        {
            readconfig();
            this.user.Text = username;
            this.pwd.Text = password;


            web.Url = new Uri("http://lgdy.whut.edu.cn/index.php?c=home&a=login");
            //CreateXmlFile("test");
        }

        private string username;

        private string password;


        private void readconfig()
        {
            var xmldoc = new XmlDocument();
            try
            {
                loadinfo();
            }
            catch (Exception)
            {
                //MessageBox.Show("create");
                CreateXmlFile("config.xml");
                loadinfo();
            }

            //Application.Exit();
        }

        private void loadinfo()
        {
            var xmldoc = new XmlDocument();
            xmldoc.Load("config.xml");
            var node = xmldoc.SelectSingleNode("Userinfo");
            var user = node.SelectSingleNode("User");
            var pass = node.SelectSingleNode("Pass");
            this.username = user.InnerText;
            this.password = pass.InnerText;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (string.IsNullOrEmpty(user.Text) || string.IsNullOrEmpty(pwd.Text)) return;
            if (web.Document != null)
            {
                HtmlElementCollection dhl = web.Document.GetElementsByTagName("button");
                    //GetElementsByTagName得到的是点击的标记元素名称
                foreach (HtmlElement item in dhl)
                {
                    if (item.InnerText == "学号／职工号登录")
                    {
                        //MessageBox.Show("ok");
                        item.InvokeMember("click");
                        var elementById = web.Document.GetElementById("username");
                        if (elementById != null)
                            elementById.InnerText = user.Text;
                        var htmlElement = web.Document.GetElementById("password");
                        if (htmlElement != null)
                            htmlElement.InnerText = pwd.Text;
                        var byId = web.Document.GetElementById("remember");
                        if (byId != null)
                            byId.InvokeMember("click");
                        click();


                        break;
                    }

                }
            }
        }

        private void click()
        {
            if (web.Document != null)
            {
                HtmlElementCollection dhl = web.Document.GetElementsByTagName("button");
                    //GetElementsByTagName得到的是点击的标记元素名称
                foreach (HtmlElement item in dhl)
                {
                    if (item.InnerText == "登　录")
                    {
                        item.InvokeMember("click");

                        break;
                    }

                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            init();
            this.WindowState=FormWindowState.Minimized;
        }

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        static extern uint GetTickCount();
        static void MySleep(uint ms)
        {
            uint start = GetTickCount();
            while (GetTickCount() - start < ms)
            {
                Application.DoEvents();
            }
        }
        /// <summary>
        /// 生成默认处理
        /// </summary>
        /// <param name="filename"></param>
        public void CreateXmlFile(string filename)
        {
            XmlTextWriter textWriter = new XmlTextWriter(filename, Encoding.UTF8);
            textWriter.Formatting = Formatting.Indented;

            textWriter.WriteStartDocument();

            textWriter.WriteComment("test");
            textWriter.WriteStartElement("Userinfo");
            textWriter.WriteElementString("User", "");
            textWriter.WriteElementString("Pass", "");
            textWriter.WriteEndDocument();
            textWriter.Close();
        }




        private void button1_Click(object sender, EventArgs e)
        {
            web.Url = new Uri("http://lgdy.whut.edu.cn/index.php?c=home&a=login");
            web.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void user_TextChanged(object sender, EventArgs e)
        {
            changexml("User", user.Text);
        }

        private void pwd_TextChanged(object sender, EventArgs e)
        {
            changexml("Pass", pwd.Text);

        }

        private void changexml(string type, string value)
        {
            var xmldoc = new XmlDocument();
            xmldoc.Load("config.xml");
            var node = xmldoc.SelectSingleNode("Userinfo");
            if (node != null)
            {
                var childnode = node.SelectSingleNode(type);
                if (childnode != null)
                    childnode.InnerText = value;
            }
            xmldoc.Save("config.xml");
        }

        

        private bool ismore = false;
        private List<Form2> list = new List<Form2>();
        private List<Process> listp = new List<Process>();
        //private int wincount=5;
        private bool isshow = false;

        private void button5_Click(object sender, EventArgs e)
        {
            if (ismore) return;
            list.Clear();
            for (int i = 0; i < winnum.Value; i++)
            {
                var a=new Process();
                a.StartInfo=new ProcessStartInfo("loginapp.exe");
                listp.Add(a);
            }
            try
            {
                foreach (var win in listp)
                {
                    win.Start();
                    MySleep((uint) delaynum.Value);
                    //win.WindowState = FormWindowState.Minimized;
                }
            }
            catch (Exception)
            {
            }
            ismore = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (!ismore) return;
            this.click();
            foreach (var win in listp)
            {
                win.Kill();
                win.Start();
            }
            foreach (var win in listp)
            {
                MySleep((uint)delaynum.Value);

                win.Start();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (!ismore) return;
            foreach (var win in listp)
            {
                win.Kill();
            }
            ismore = false;

        }

        private void winnum_ValueChanged(object sender, EventArgs e)
        {
            if (winnum.Value < 0)
            {
                winnum.Value = 0;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            button6_Click(sender, e);
        }
    }
}
